﻿//	Copyright (c) 2011-2012 Jonathan Loe
//
//	MIT license (http://en.wikipedia.org/wiki/MIT_License)
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights 
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//	of the Software, and to permit persons to whom the Software is furnished to do so, 
//	subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in all 
//	copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
//	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//	FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
//	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

using Ordinaire.Data;
using Ordinaire.Security;

namespace Ordinaire.Taxonomy.Data
{
    /// <summary>
    /// Used to represent a table to store item tags that derives Data<int, string>.
    /// </summary>
    [Table]
    public class Tags : ReferenceData<Vocabs, int, string>
    {
        #region Variables

        /// <summary>
        /// Entity id.
        /// </summary>
        private int entityId;

        /// <summary>
        /// Entity object type.
        /// </summary>
        private string entityType;

        /// <summary>
        /// Entity assembly.
        /// </summary>
        private string entityAssembly;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the unique identifier.
        /// </summary>
        [Column(IsPrimaryKey = true, IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
        public override int Id
        {
            get { return base.Id; }
            set { base.Id = value; }
        }

        /// <summary>
        /// Gets or sets the entity id.
        /// </summary>
        [Column(DbType = "int NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public int EntityId
        {
            get { return this.entityId; }
            set { this.entityId = value; }
        }

        /// <summary>
        /// Gets or sets the entity object type.
        /// </summary>
        [Column(DbType = "varchar(max) NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public string EntityType
        {
            get { return this.entityType; }
            set { this.entityType = value; }
        }

        /// <summary>
        /// Gets or sets the entity assembly name.
        /// </summary>
        [Column(DbType = "nvarchar(max) NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public string EntityAssembly
        {
            get { return this.entityAssembly; }
            set { this.entityAssembly = value; }
        }

        /// <summary>
        /// Gets or sets the related vocab id.
        /// </summary>
        [Column(Name = "VocabId", DbType = "int NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public override int ForeignId
        {
            get { return base.ForeignId; }
            set { base.ForeignId = value; }
        }

        /// <summary>
        /// Get or set the username who created this entry.
        /// </summary>
        [Column(DbType = "nvarchar(10) NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public override string CreatedBy
        {
            get { return base.CreatedBy; }
            set { base.CreatedBy = value.TrimOrEmpty(); }
        }

        /// <summary>
        /// Get or set the date when the entry was first created.
        /// </summary>
        [Column(DbType = "datetime NOT NULL DEFAULT(GETDATE())", UpdateCheck = UpdateCheck.Never)]
        public override DateTime? CreateDate
        {
            get { return base.CreateDate; }
            set { base.CreateDate = value; }
        }

        /// <summary>
        /// Get or set the username who last updated this entry.
        /// </summary>
        [Column(DbType = "nvarchar(10) NULL", CanBeNull = true, UpdateCheck = UpdateCheck.Never)]
        public override string UpdatedBy
        {
            get { return base.UpdatedBy; }
            set { base.UpdatedBy = value.TrimOrEmpty(); }
        }

        /// <summary>
        /// Get or set the date when the entry was last updated.
        /// </summary>
        [Column(DbType = "datetime NULL", CanBeNull = true, UpdateCheck=UpdateCheck.Never)]
        public override DateTime? UpdateDate
        {
            get { return base.UpdateDate; }
            set { base.UpdateDate = value; }
        }

        #endregion

        #region Associations

        /// <summary>
        /// Gets or sets the associated data from table 'Vocabs'.
        /// </summary>
        [Association(Name = "FK_Tags_Vocabs", IsForeignKey = true, ThisKey = "ForeignId", Storage = "foreign")]
        public override Vocabs Foreign
        {
            get { return base.Foreign; }
            set { base.Foreign = value; }
        }

        #endregion

        #region Expressions

        /// <summary>
        /// Returns a related expression where entity is equal to the specified one.
        /// </summary>
        /// <param name="item">the item to compare</param>
        /// <returns>the related expression</returns>
        public static Expression<Func<Tags, bool>> EntityIs(Entity<int> item)
        {
            return d => d.EntityType == item.GetType().FullName && d.EntityId == item.Id;
        }

        /// <summary>
        /// Returns a related expression where entity type is equals to the specified one.
        /// </summary>
        /// <param name="type">the entity type</param>
        /// <returns>the related expression</returns>
        public static Expression<Func<Tags, bool>> EntityIs(Type type)
        {
            return d => d.EntityType == type.FullName;
        }

        /// <summary>
        /// Returns a related expression where vocab is equal to the specified one.
        /// </summary>
        /// <param name="id">the vocab id to compare</param>
        /// <returns>the related expression</returns>
        public static Expression<Func<Tags, bool>> VocabIdIs(int id)
        {
            return d => d.ForeignId == id;
        }

        #endregion
    }
}
